package com.leancloud.login.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.aries.ui.view.title.TitleBarView;
import com.leancloud.login.LoginPath;
import com.leancloud.login.R;
import com.leancloud.login.user.LoginBack;
import com.leancloud.login.user.UserGlobal;
import com.vise.xsnow.event.BusManager;

import tech.com.commoncore.LoginInterceptorImpl;
import tech.com.commoncore.base.BaseTitleActivity;
import tech.com.commoncore.event.LoginChangeEvent;
import tech.com.commoncore.user.CurrentUser;
import tech.com.commoncore.utils.ToastUtil;

@Route(path = LoginPath.LOGIN_SIGNATURE, extras = LoginInterceptorImpl.LOGINCHECK)
public class SingeInputActivity extends BaseTitleActivity {

    private EditText singeInputEt;

    @Override
    public void setTitleBar(TitleBarView titleBar) {
        titleBar.setTitleMainText("编辑签名")
                .setRightText("确定")
                .setOnRightTextClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        inputSinge();
                    }
                });
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_singe_input;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        singeInputEt = findViewById(R.id.singe_input_et);
    }

    private void inputSinge() {
        String singe = singeInputEt.getText().toString();
        CurrentUser user = UserGlobal.getUserImp().getCurrentUser();
        if (user == null) {
            ToastUtil.show("账号未登录");
            return;
        }
        showLoading("正在修改中......");
        UserGlobal.getUserImp().modifyUserSignature(user.getToken(), singe, new LoginBack() {
            @Override
            public void onBack(Object o, String err) {
                hideLoading();
                if (err == null) {
                    ToastUtil.show("修改成功");
                    finish();
                    BusManager.getBus().post(new LoginChangeEvent(true));
                } else {
                    ToastUtil.show("修改失败");
                }
            }
        });
    }
}
